<template>
  <div class="app-container">
    <PageSearch :showSearch.sync="showSearch" :getList="getList" :handleQuery="handleQuery" :resetQuery="resetQuery">
      <template #pagination>
        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </template>
      <template #form>
        <el-form :model="queryParams" ref="queryForm" size="small">
          <el-row>
            <el-form-item prop="cesNo">
              <el-input
                :placeholder="$t('耗材编号')"
                v-model="queryParams.cesNo"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item prop="cesName">
              <el-input
                :placeholder="$t('名称')"
                v-model="queryParams.cesName"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item prop="cesPositionNo">
              <el-input
                :placeholder="$t('库位')"
                v-model="queryParams.cesPositionNo"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-row>
        </el-form>
      </template>
      <template #actions>
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['distribution:skuConsumablePurchase:remove']"
        >
          {{ $t('删除') }}
        </el-button>
        <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['distribution:skuConsumablePurchase:export']"
          >导出</el-button
        >
      </el-col> -->
      </template>
    </PageSearch>

    <el-table
      :height="calTableHeight()"
      v-loading="loading"
      ref="TableRef"
      :data="skuConsumablePurchaseList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column show-overflow-tooltip type="selection" width="55" align="center" />
      <el-table-column show-overflow-tooltip label="耗材编号" align="center" prop="cesNo" width="180" />
      <el-table-column show-overflow-tooltip label="名称" align="center" prop="cesName" width="150" />
      <el-table-column show-overflow-tooltip label="库位" align="center" prop="cesPositionNo" />
      <el-table-column show-overflow-tooltip :label="$t('采购数量')" align="center" prop="num" />
      <el-table-column show-overflow-tooltip label="付款总价" align="center" prop="priceTotal" width="150">
        <!-- 付款总价单位 1-RMB 2-JPY 3-USD -->
        <template slot-scope="{ row }">
          {{ row.priceTotal ? (+row.priceTotal).toFixed(2) : '' }}
          <span v-if="row.priceUnit == 1">RMB</span>
          <span v-if="row.priceUnit == 2">JPY</span>
          <span v-if="row.priceUnit == 3">USD</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="产品单价(JPY)" align="center" prop="cesUnitPrice">
        <!-- 付款总价单位 1-RMB 2-JPY 3-USD -->
        <template slot-scope="{ row }">
          {{ row.cesUnitPrice ? (+row.cesUnitPrice).toFixed() : '' }}
        </template>
      </el-table-column>

      <el-table-column show-overflow-tooltip label="产品总货值(JPY)" align="center" prop="cesTotalAmount">
        <template slot-scope="{ row }">
          <div>
            {{ row.cesTotalAmount | thousand }}
          </div>
        </template>
      </el-table-column>

      <el-table-column show-overflow-tooltip label="采购备注" align="center" prop="purchaseRemark" />
      <el-table-column show-overflow-tooltip label="退款数量" align="center" prop="refundNum" />
      <el-table-column show-overflow-tooltip label="退款金额" align="center" prop="refundPrice" width="150">
        <!-- 退款金额单位1-RMB 2-JPY 3-USD -->
        <template slot-scope="{ row }">
          {{ row.refundPrice ? (+row.refundPrice).toFixed() : '' }}
          <span v-if="row.refundPriceUnit == 1">RMB</span>
          <span v-if="row.refundPriceUnit == 2">JPY</span>
          <span v-if="row.refundPriceUnit == 3">USD</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="退款原因" align="center" prop="refundReason" />
      <el-table-column show-overflow-tooltip :label="$t('收货数量')" align="center" prop="receiveNum">
        <template slot-scope="{ row }">
          <div style="color: #13ce66; cursor: pointer" @click="handleReceiveGoodsNum(row)" v-if="row.receiveNum">
            {{ row.receiveNum }}
          </div>
          <div style="color: #409eff; cursor: pointer" v-else @click="handleReceiveGoodsNum(row)">未收货</div>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="采购链接" align="center" prop="purchaseLink" width="200">
        <template slot-scope="{ row }">
          <el-link :href="row.purchaseLink" target="_blank" type="success">
            {{ row.purchaseLink }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip :label="$t('备注')" align="center" prop="remark" />
      <el-table-column
        show-overflow-tooltip
        :label="$t('操作')"
        align="center"
        fixed="right"
        :width="calWidth(150, 200, 150)"
      >
        <template slot-scope="scope">
          <!-- 收货了不能修改 -->
          <el-button
            size="mini"
            v-show="!scope.row.receiveNum"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['distribution:skuConsumablePurchase:edit']"
          >
            修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['distribution:skuConsumablePurchase:remove']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品SKU耗材采购对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="耗材编号" prop="cesNo">
          <el-input v-model="form.cesNo" readonly placeholder="请输入耗材编号" />
        </el-form-item>
        <el-form-item :label="$t('采购数量')" prop="num">
          <el-input type="number" v-model.number="form.num" placeholder="请输入采购数量" />
        </el-form-item>
        <el-form-item label="付款总价" prop="priceTotal">
          <el-input type="number" v-model.number="form.priceTotal" placeholder="请输入付款总价" />
        </el-form-item>
        <el-form-item label="付款总价单位" prop="priceUnit">
          <el-select style="width: 100%" clearable v-model="form.priceUnit">
            <el-option label="RMB" :value="1" />
            <el-option label="JPY" :value="2" />
            <el-option label="USD" :value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="产品总货值" prop="cesTotalAmount">
          <el-input-number
            style="width: 100%"
            :precision="0"
            v-model="form.cesTotalAmount"
            controls-position="right"
            :min="0"
          />
        </el-form-item>
        <el-form-item label="产品单价(JPY)" prop="cesUnitPrice">
          <el-input-number
            style="width: 100%"
            :precision="0"
            v-model="form.cesUnitPrice"
            controls-position="right"
            :min="0"
          />
        </el-form-item>
        <el-form-item label="采购备注" prop="purchaseRemark">
          <el-input v-model="form.purchaseRemark" placeholder="请输入采购备注" />
        </el-form-item>
        <el-form-item label="退款数量" prop="refundNum">
          <el-input type="number" v-model.number="form.refundNum" placeholder="请输入退款数量" />
        </el-form-item>
        <el-form-item label="退款金额" prop="refundPrice">
          <el-input type="number" v-model.number="form.refundPrice" placeholder="请输入退款金额" />
        </el-form-item>
        <el-form-item label="退款金额单位" prop="refundPriceUnit">
          <el-select style="width: 100%" clearable v-model="form.refundPriceUnit">
            <el-option label="RMB" :value="1" />
            <el-option label="JPY" :value="2" />
            <el-option label="USD" :value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="退款原因" prop="refundReason">
          <el-input v-model="form.refundReason" placeholder="请输入退款原因" />
        </el-form-item>
        <el-form-item :label="$t('收货数量')" prop="receiveNum">
          <el-input type="number" v-model.number="form.receiveNum" placeholder="请输入收货数量" />
        </el-form-item>
        <el-form-item label="采购链接" prop="purchaseLink">
          <el-input v-model="form.purchaseLink" placeholder="请输入采购链接" />
        </el-form-item>
        <el-form-item :label="$t('备注')">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 收货数量 -->
    <ReceiveGoodsNum :show.sync="receiveGoodsNum.show" :formData="receiveGoodsNum.form" @success="getList" />
  </div>
</template>

<script>
import {
  listSkuConsumablePurchase,
  getSkuConsumablePurchase,
  delSkuConsumablePurchase,
  addSkuConsumablePurchase,
  updateSkuConsumablePurchase,
  getCesNo,
} from '@/api/distribution/sku/skuConsumablePurchase'
import ReceiveGoodsNum from './components/receiveGoodsNum'
export default {
  activated() {
    this.$nextTick(() => this.$refs['TableRef']?.doLayout())
  },
  name: 'SkuConsumablePurchase',
  components: { ReceiveGoodsNum },
  data() {
    return {
      /**收货数量 */
      receiveGoodsNum: {
        show: false,
        form: {},
      },
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 商品SKU耗材采购表格数据
      skuConsumablePurchaseList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cesNo: '',
        cesName: '',
        cesPositionNo: '',
      },
      // 表单参数
      form: {
        /**耗材编号 */
        cesNo: '',
        num: '',
        priceTotal: '',
        priceUnit: '',
        cesTotalAmount: '',
        cesUnitPrice: '',
        purchaseRemark: '',
        refundNum: '',
        refundPrice: '',
        refundPriceUnit: '',
        refundReason: '',
        receiveNum: '',
        purchaseLink: '',
        remark: '',
      },
      // 表单校验
      rules: {
        id: [{ required: true, message: 'ID不能为空', trigger: 'blur' }],
        cesNo: [{ required: true, message: '耗材编号不能为空', trigger: 'blur' }],
        num: [{ required: true, message: '采购数量不能为空', trigger: 'blur' }],
        priceTotal: [{ required: true, message: '付款总价不能为空', trigger: 'blur' }],
        priceUnit: [
          {
            required: true,
            message: '付款总价单位 1-RMB 2-JPY 3-USD不能为空',
            trigger: 'blur',
          },
        ],
        cesTotalAmount: [{ required: true, message: '产品总货值不能为空', trigger: 'blur' }],
        cesUnitPrice: [{ required: true, message: '产品单价不能为空', trigger: 'blur' }],
        // purchaseRemark: [{ required: true, message: '采购备注不能为空', trigger: 'blur' }],
        // refundNum: [{ required: true, message: '退款数量不能为空', trigger: 'blur' }],
        // refundPrice: [{ required: true, message: '退款金额不能为空', trigger: 'blur' }],
        // refundPriceUnit: [
        //   {
        //     required: true,
        //     message: '退款金额单位 1-RMB 2-JPY 3-USD不能为空',
        //     trigger: 'blur',
        //   },
        // ],
        // refundReason: [{ required: true, message: '退款原因不能为空', trigger: 'blur' }],
        // receiveNum: [{ required: true, message: '收货数量不能为空', trigger: 'blur' }],
        // purchaseLink: [{ required: true, message: '采购链接不能为空', trigger: 'blur' }],
        // remark: [{ required: true, message: '备注不能为空', trigger: 'blur' }],
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleReceiveGoodsNum(row) {
      this.receiveGoodsNum.show = true
      this.receiveGoodsNum.form = JSON.parse(JSON.stringify(row))
    },
    /** 查询商品SKU耗材采购列表 */
    getList() {
      this.loading = true
      listSkuConsumablePurchase(this.queryParams).then((response) => {
        this.skuConsumablePurchaseList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      getCesNo().then((res) => {
        const { code, data, msg } = res
        if (code === 200) {
          console.log(`res ==>`, res)
          this.form.cesNo = msg
          this.open = true
          this.title = this.$t('添加')
        }
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true
      this.reset()
      const id = row.id || this.ids
      getSkuConsumablePurchase(id).then((response) => {
        this.loading = false
        this.form = response.data
        console.log(` ==>`, JSON.parse(JSON.stringify(this.form)))
        this.open = true
        this.title = this.$t('编辑')
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.buttonLoading = true
          if (this.form.id != null) {
            updateSkuConsumablePurchase(this.form)
              .then((response) => {
                this.$modal.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
              .finally(() => {
                this.buttonLoading = false
              })
          } else {
            addSkuConsumablePurchase(this.form)
              .then((response) => {
                this.$modal.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
              .finally(() => {
                this.buttonLoading = false
              })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal
        .confirm('是否确认删除商品SKU耗材采购编号为"' + ids + '"的数据项？')
        .then(() => {
          this.loading = true
          return delSkuConsumablePurchase(ids)
        })
        .then(() => {
          this.loading = false
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'distribution/skuConsumablePurchase/export',
        {
          ...this.queryParams,
        },
        `skuConsumablePurchase_${new Date().getTime()}.xlsx`,
      )
    },
  },
}
</script>
